package com.acme.bankapp.service.bank;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.acme.bankapp.domain.bank.account.Account;
import com.acme.bankapp.domain.bank.account.CheckingAccount;
import com.acme.bankapp.domain.bank.client.Client;

public class BankReport {
	public static void getNbOfCustomers(Bank bank) {
		System.out.println("Count of clients: " + bank.getClients().size());
	}

	public static void getNbOfAccounts(Bank bank) {
		int countAccounts = 0;
		for (Client client : bank.getClients()) {
			countAccounts += client.getAccounts().size();
		}
		System.out.println("Count of accounts of all clients: " + countAccounts);
	}

	public static void getAccountsSortedBySum(Bank bank) {
		List<Account> accounts = new LinkedList<Account>();
		for (Client client : bank.getClients()) {
			accounts.addAll(client.getAccounts());
		}
		Collections.sort(accounts, new Comparator<Account>() {

			@Override
			public int compare(Account o1, Account o2) {
				return Double.compare(o1.maximumAmountToWithdraw(), o2.maximumAmountToWithdraw());
			}});

		System.out.println("Accounts sort by amount: min < ");
		for (Account account : accounts) {
			System.out.print(account + " < ");
		}
		System.out.println("max");
	}

	public static void getBankCredit(Bank bank) {
		double credit = 0;
		for (Client client : bank.getClients()) {
			for (Account account : client.getAccounts()) {
				if (account instanceof CheckingAccount) {
					if (account.getBalance() < 0) {
						credit += account.getBalance();
					}
				}
			}
		}
		System.out.println("Credit by bank: " + credit);
	}

	public static Map<Client, Account[]> getCustomerAccount(Bank bank) {
		Map<Client, Account[]> map = new HashMap<Client, Account[]>();

		for (Client client : bank.getClients()) {
			Account[] accounts = new Account[client.getCountAccounts()];
			map.put(client, client.getAccounts().toArray(accounts));
		}

		return map;
	}
}
